package com.example.meet.ui;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.viewpager.widget.ViewPager;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.framework.base.BaseActivity;
import com.example.framework.base.baseAdapter.BasePageAdapter;
import com.example.framework.manager.MediaPlayerManager;
import com.example.framework.utils.AnimUtils;
import com.example.meet.MainActivity;
import com.example.meet.R;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 */
public class GuideActivity extends BaseActivity implements View.OnClickListener {
    /**
     * 1. ViewPage ：适配器 | 帧动画播放
     * 2. 小圆点逻辑
     * 3. 歌曲的播放
     * 4. 属性动画旋转
     * 5. 跳转
     */

    private ImageView iv_music_switch;
    private TextView tv_guide_skip;
    private ImageView iv_guide_point1;
    private ImageView iv_guide_point2;
    private ImageView iv_guide_point3;
    private ViewPager mViewPager;

    // 3个View，定义ViewPager
    private View view1;
    private View view2;
    private View view3;
    // 使用一个list装载
    private List<View> mPageList = new ArrayList<>();

    private BasePageAdapter mAdapter;

    private ImageView iv_guide_star;
    private ImageView iv_guide_night;
    private ImageView tv_guide_smile;

    private MediaPlayerManager mGuideMusic;

    private ObjectAnimator mAnim;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initView();
    }


    private void initView() {
        iv_music_switch = findViewById(R.id.iv_music_switch);
        tv_guide_skip = findViewById(R.id.tv_guide_skip);
        iv_guide_point1 = findViewById(R.id.iv_guide_point1);
        iv_guide_point2 = findViewById(R.id.iv_guide_point2);
        iv_guide_point3 = findViewById(R.id.iv_guide_point3);
        mViewPager = findViewById(R.id.mViewPager);

        iv_music_switch.setOnClickListener(this);
        tv_guide_skip.setOnClickListener(this);

        // 加载三个ViewPager
        view1 = View.inflate(this, R.layout.layout_pager_guide_1, null);
        view2 = View.inflate(this, R.layout.layout_pager_guide_2, null);
        view3 = View.inflate(this, R.layout.layout_pager_guide_3, null);

        // 帧动画
        iv_guide_star = view1.findViewById(R.id.iv_guide_star);
        iv_guide_night = view2.findViewById(R.id.iv_guide_night);
        tv_guide_smile = view3.findViewById(R.id.iv_guide_smile);

        // 帧动画播放
        AnimationDrawable animStar = (AnimationDrawable) iv_guide_star.getBackground();
        animStar.start();
        AnimationDrawable animNight = (AnimationDrawable) iv_guide_night.getBackground();
        animNight.start();
        AnimationDrawable animSmile = (AnimationDrawable) tv_guide_smile.getBackground();
        animSmile.start();

        mPageList.add(view1);
        mPageList.add(view2);
        mPageList.add(view3);

        // 预加载
        mViewPager.setOffscreenPageLimit(mPageList.size());

        mAdapter = new BasePageAdapter(mPageList);
        mViewPager.setAdapter(mAdapter);

        // 小圆点逻辑，用来监听ViewPager
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedPoint(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        // 播放歌曲的逻辑
        startMusic();

    }

    private void startMusic() {
        mGuideMusic = new MediaPlayerManager();
        mGuideMusic.setLooping(true);
        AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.guide);
        mGuideMusic.startPlayer(file);

        // 旋转动画
        ObjectAnimator mAnim = AnimUtils.rotation(iv_music_switch);
        mAnim.start();
    }

    /**
     * 动态选择小圆点
     * @param position
     */
    private void selectedPoint(int position) {
        switch (position) {
            case 0:
                iv_guide_point1.setImageResource(R.drawable.img_guide_point_p);
                iv_guide_point2.setImageResource(R.drawable.img_guide_point);
                iv_guide_point3.setImageResource(R.drawable.img_guide_point);
                break;
            case 1:
                iv_guide_point1.setImageResource(R.drawable.img_guide_point);
                iv_guide_point2.setImageResource(R.drawable.img_guide_point_p);
                iv_guide_point3.setImageResource(R.drawable.img_guide_point);
                break;
            case 2:
                iv_guide_point1.setImageResource(R.drawable.img_guide_point);
                iv_guide_point3.setImageResource(R.drawable.img_guide_point_p);
                iv_guide_point2.setImageResource(R.drawable.img_guide_point);
                break;

        }
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.iv_music_switch:
                if (mGuideMusic.MEDIA_STATUS == MediaPlayerManager.MEDIA_STATUS_PAUSE) {
                    mAnim.start();
                    mGuideMusic.continuePlay();
                    iv_music_switch.setImageResource(R.drawable.img_guide_music);
                } else if (mGuideMusic.MEDIA_STATUS == MediaPlayerManager.MEDIA_STATUS_PLAY) {
                    mAnim.pause();
                    mGuideMusic.pausePlayer();
                    iv_music_switch.setImageResource(R.drawable.img_guide_music_off);
                }
                break;
            case R.id.tv_guide_skip:
                startActivity(new Intent(this, IndexActivity.class));
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mGuideMusic.stopPlayer();
    }
}